package util

import (
	"encoding/json"
	"fmt"
	dto "goweb/dto"
	"net/http"
	"os"
	"strings"
)

//ReturnJSON 返回json字符串
func ReturnJSON(w http.ResponseWriter, v interface{}) {
	w.Header().Set("Content-Type", "application/json;charset=utf-8")
	json.NewEncoder(w).Encode(v)
}

//PkgResult 封装结果
func PkgResult(suc bool, code string, msg string, data interface{}) *dto.Result {
	return &dto.Result{Code: code, Suc: suc, Msg: msg, Data: data}
}

//GetPath 获取新项目路径
func GetPath() string {
	path := getCurrentPath()
	return path
}

func getCurrentPath() string {
	fmt.Println("aaaaaaaaa:", os.Args[0])
	s := os.Args[0]
	// s, err := exec.LookPath(os.Args[0])
	// checkErr(err)
	// fmt.Println("sssss:", s)
	i := strings.LastIndex(s, "\\")
	path := string(s[0 : i+1])
	return path
}

func checkErr(err error) {
	if err != nil {
		fmt.Println(err)
	}
}
